<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>JWT 登录示例</title>
</head>
<body>
<h1>JWT 认证演示</h1>

<button onclick="login()">登录</button>
<button onclick="getInfo()">获取用户信息</button>

<script>
    // 登录函数
    function login() {
        fetch('http://localhost:9000/JWT/login?username=admin&password=123456', {
            method: 'POST'
        })
            .then(res => res.json())
            .then(data => {
                if (data.token) {
                    localStorage.setItem('jwt_token', data.token);
                    alert('登录成功: ' + data.msg);
                } else {
                    alert('登录失败: ' + data.msg);
                }
            });
    }

    // 获取用户信息函数
    function getInfo() {
        const token = localStorage.getItem('jwt_token');

        if (!token) {
            alert('请先登录！');
            return;
        }

        fetch('http://localhost:9000/JWT/info', {
            headers: {
                'Authorization': token
            }
        })
            .then(res => res.json())
            .then(data => {
                if (data.username) {
                    alert('用户名: ' + data.username + '\n状态: ' + data.msg);
                } else {
                    alert('错误: ' + data.msg);
                }
            });
    }
</script>
</body>
</html>